dnl DNA define([AC_CACHE_LOAD], )dnl

AC_INIT([tsung], m4_normalize(m4_include([vsn.mk])),[tsung-users@process-one.net])
AC_PREREQ(2.59c)
AC_COPYRIGHT(Copyright (C) 2008 Nicolas Niclausse)
AC_CONFIG_SRCDIR(src/tsung/tsung.erl)
dnl AM_INIT_AUTOMAKE()

AC_CACHE_LOAD

AC_SUBST([CONFIG_STATUS_DEPENDENCIES],[vsn.mk])
AC_SUBST([CONFIGURE_DEPENDENCIES],[vsn.mk])

AC_PATH_PROG(SED, sed)

AC_LANG(Erlang)
AC_ARG_WITH(erlang, [  --with-erlang=PREFIX    path to erlc and erl ])
AC_ERLANG_PATH_ERLC(erlc, $with_erlang:$with_erlang/bin:$PATH)
AC_ERLANG_PATH_ERL(erl, $with_erlang:$with_erlang/bin:$PATH)

AC_PATH_PROG(DIALYZER,dialyzer, /usr/bin/dializer, $with_erlang:$with_erlang/bin:$PATH)

AC_PREFIX_PROGRAM(erl)
AC_ERLANG_SUBST_ROOT_DIR()

dnl check for xmerl include path
AC_ERLANG_CHECK_LIB(xmerl)
AC_ERLANG_CHECK_LIB(ssl)
AC_ERLANG_CHECK_LIB(crypto)
AC_ERLANG_CHECK_LIB(public_key)

dnl check if ssl is working
AC_CACHE_CHECK([if Erlang/OTP SSL application is running fine],
    [erlang_cv_ssl_runnable],
    [erlang_cv_ssl_runnable=no
     AC_RUN_IFELSE(
        [AC_LANG_PROGRAM([], [dnl
         case application:start(ssl) of
           ok  -> ok;
           Err -> halt(1)
         end,
         halt(0)])],
        [erlang_cv_ssl_runnable=yes
         ERLANG_APPLICATIONS="kernel,stdlib,ssl"],
        [
            AC_RUN_IFELSE(
            [AC_LANG_PROGRAM([], [dnl
               application:start(crypto),
               application:start(asn1),
               application:start(public_key),
               case application:start(ssl) of
                    ok  -> ok;
                    Err -> halt(1)
               end,
               halt(0)])],
               [erlang_cv_ssl_runnable=yes
                ERLANG_APPLICATIONS="kernel,stdlib,asn1,crypto,public_key,ssl"],
                [ERLANG_APPLICATIONS="kernel,stdlib"
                 AC_MSG_RESULT(WARNING: ssl application is not working properly !!!)])
        ])
    ])



dnl check if crypto is working
AC_CACHE_CHECK([if Erlang/OTP crypto application is running fine],
    [erlang_cv_crypto_runnable],
    [erlang_cv_crypto_runnable=no
     AC_RUN_IFELSE(
        [AC_LANG_PROGRAM([], [dnl
         case application:start(crypto) of
           ok ->
                 case catch crypto:hash(md5, "toto") of
                   <<247,29,190,82,98,138,63,131,167,122,180,148,129,117,37, 198>> -> ok;
                   _ ->
                    halt(1)
                 end;
           Err ->
             erlang:display([Err]),
             halt(1)
         end,
         halt(0) ])],
        [ erlang_cv_crypto_runnable=yes
          ERLANG_APPLICATIONS="$ERLANG_APPLICATIONS,crypto" ],
        [ AC_MSG_RESULT([WARNING: crypto application is not working properly !!!])]) ])

dnl check if new time API is available (R18 and up)
AC_CACHE_CHECK([new time API],
    [erlang_cv_new_time_api],
    [erlang_cv_new_time_api=no
     AC_RUN_IFELSE(
        [AC_LANG_PROGRAM([], [dnl
         R=case catch erlang:timestamp() of
           {A,B,C}  -> 0;
           _        -> 1
         end,
         halt(R)])],
        [erlang_cv_new_time_api=yes],
        [AC_MSG_RESULT(WARNING: new time API not available. use old now() instead)])
    ])


AC_SUBST(erlang_cv_new_time_api)
AC_SUBST(ERL_OPTS)
AC_SUBST(ERLANG_APPLICATIONS)
AC_SUBST(DTD,[tsung-1.0.dtd])
AC_SUBST(TEMPLATES_SUBDIR,[tsung/templates])

AC_PROG_MAKE_SET
AC_PROG_INSTALL

AS_AC_EXPAND(EXPANDED_LIBDIR, "$libdir")
AC_MSG_NOTICE(Storing library files in $EXPANDED_LIBDIR)
AS_AC_EXPAND(EXPANDED_SHAREDIR, "$datadir/tsung")
AC_MSG_NOTICE(Storing data files in $EXPANDED_SHAREDIR)

AC_CONFIG_FILES([\
        Makefile \
        tsung.spec \
        tsung.sh \
        tsung-recorder.sh \
        examples/*.xml \
        src/tsung_stats.pl \
        src/tsung-plotter/tsplot.py \
        src/log2tsung.pl \
        src/tsung_controller/tsung_controller.app \
        src/tsung_recorder/tsung_recorder.app \
        src/tsung/tsung.app \
        ])

AC_OUTPUT
